﻿namespace AddressBook.Business.Contact
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	[Serializable]
	public class ContactPhoneNumbers : BusinessListBase<ContactPhoneNumbers, ContactPhoneNumber>
  {
    #region "Business Methods"

    public ContactPhoneNumber GetItem(Int32 phoneNumberId)
    {
      foreach (ContactPhoneNumber phoneNumber in this)
      {
        if (phoneNumber.PhoneNumberId == phoneNumberId)
        {
          return phoneNumber;
        }
      }

      return null;
    }

    public void Add(Int32 phoneNumberTypeId)
    {
      if (!(Contains(phoneNumberTypeId)))
      {
        ContactPhoneNumber resource = ContactPhoneNumber.NewPhoneNumber(phoneNumberTypeId);
        this.Add(resource);
      }
      else
      {
        throw new InvalidOperationException("Contact already has a phone number of that type");
      }
    }

    public void Add(String phoneNumber, Int32 phoneNumberTypeId)
    {
      if (!(Contains(phoneNumberTypeId)))
      {
        ContactPhoneNumber resource = ContactPhoneNumber.NewPhoneNumber(phoneNumber, phoneNumberTypeId);
        this.Add(resource);
      }
      else
      {
        throw new InvalidOperationException("Contact already has a phone number of that type");
      }
    }

    public void Remove(Int32 phoneNumberId)
    {
      foreach (ContactPhoneNumber phoneNumber in this)
      {
        if (phoneNumber.PhoneNumberId == phoneNumberId)
        {
          Remove(phoneNumber);
          break;
        }
      }
    }

    public bool Contains(Int32 phoneNumberTypeId)
    {
      foreach (ContactPhoneNumber phoneNumber in this)
      {
        if (phoneNumber.PhoneNumberTypeId == phoneNumberTypeId)
        {
          return true;
        }
      }
      return false;
    }

    public bool ContainsDeleted(Int32 phoneNumberTypeId)
    {
      foreach (ContactPhoneNumber phoneNumber in DeletedList)
      {
        if (phoneNumber.PhoneNumberTypeId == phoneNumberTypeId)
        {
          return true;
        }
      }
      return false;
    }

    #endregion

    #region "Factory Methods"

    internal static ContactPhoneNumbers NewContactPhoneNumbers()
    {
      return DataPortal.CreateChild<ContactPhoneNumbers>();
    }

		internal static ContactPhoneNumbers GetContactPhoneNumbers(AddressBook.Business.Model.ContactPhoneNumber[] data)
    {
      return DataPortal.FetchChild<ContactPhoneNumbers>(data);
    }

    private ContactPhoneNumbers()
    { /* require use of factory methods */ }

    #endregion

    #region "Data Access"

    private void Child_Fetch(AddressBook.Business.Model.ContactPhoneNumber[] data)
    {
      if (data == null) return;

      this.RaiseListChangedEvents = false;

      foreach (var value in data)
      {
        this.Add(ContactPhoneNumber.GetPhoneNumber(value));
      }

      this.RaiseListChangedEvents = true;
    }

    #endregion
  }
}
